package com.fsnbigdata.repository;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.ProductEnterprise;
import com.fsnbigdata.domain.Product;
import com.fsnbigdata.domain.QProductEnterprise;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = ProductEnterpriseRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = ProductEnterpriseRepositoryCustom.class)
@Transactional(readOnly = true)
public class ProductEnterpriseRepositoryImpl extends QueryDslRepositorySupportExt<ProductEnterprise> implements ProductEnterpriseRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    ProductEnterpriseRepositoryImpl() {
        super(ProductEnterprise.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PRODUCT = "product";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ENTERPRISE_ID = "enterpriseId";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    /**
     * TODO Auto-generated method documentation
     *
     * @param product
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<ProductEnterprise> findByProduct(Product product, GlobalSearch globalSearch, Pageable pageable) {
        QProductEnterprise productEnterprise = QProductEnterprise.productEnterprise;
        JPQLQuery<ProductEnterprise> query = from(productEnterprise);
        Assert.notNull(product, "product is required");
        query.where(productEnterprise.product.eq(product));
        Path<?>[] paths = new Path<?>[] { productEnterprise.product, productEnterprise.enterpriseId, productEnterprise.name };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(PRODUCT, productEnterprise.product).map(ENTERPRISE_ID, productEnterprise.enterpriseId).map(NAME, productEnterprise.name);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, productEnterprise);
    }
}
